package cn.stylefeng.roses.kernel.conversion.util;

import cn.stylefeng.roses.kernel.conversion.exception.DocumentParseExceptionEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

import java.io.ByteArrayInputStream;
import java.io.OutputStream;

/**
 * ppt工具类
 *
 * @author fengshuonan
 * @since 2023/10/20 22:58
 */
public class PptUtil {

    /**
     * ppt转化为pdf，读入字节，输出到流中
     *
     * @param inputFileBytes 原有文件字节数组
     * @author fengshuonan
     * @since 2023/10/20 22:48
     */
    public static void toPdfByBytes(byte[] inputFileBytes, String suffix, OutputStream outputStream) {
        // 创建一个Presentation实例
        Presentation presentation = new Presentation();

        // 加载示例 ODP 文档
        try {
            presentation.loadFromStream(new ByteArrayInputStream(inputFileBytes), FileFormat.ODP);
        } catch (Exception e) {
            throw new ServiceException(DocumentParseExceptionEnum.EXCEPTION_FILE_IO_ERROR);
        }

        // 将文档保存为 PDF
        presentation.saveToFile(outputStream, FileFormat.PDF);
    }

}
